﻿@{
    Layout = null;
}

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Administration: Login</title>
     <link href="@Url.Content("~/Content/style/admin/style.css")" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="@Url.Content("~/Scripts/jquery-1.6.4.min.js")"></script>
    <script type="text/javascript" src="@Url.Content("~/Scripts/jquery.validate.min.js")"></script>
    <script type="text/javascript" src="@Url.Content("~/Scripts/jquery.cookie.js")"></script>

    <script type="text/javascript">
        $(document).ready(function () {

            $('#txtUsername').focus();
            $("#btnLogin").click(function () {

                DoLogin();
            });

            $('#loginForm input').bind('keypress', function (e) {
                if (e.keyCode == 13) {

                    DoLogin();
                }
            });

            if ($.cookie('mySealightCookie') != null) {
               
                try {
                    var cookies = $.cookie('mySealightCookie').split('&');
                    var name = 'username';
                    var emailValue = '';
                    var pwd_name = 'password';
                    var encrytedPwd = '';
                    for (var i = 0; i < cookies.length; i++) {
                        var cookie = jQuery.trim(cookies[i]);
                        if (cookie.substring(0, name.length + 1) == (name + '=')) {
                            emailValue = decodeURIComponent(cookie.substring(name.length + 1));                          
                        }
                        if (cookie.substring(0, pwd_name.length + 1) == (pwd_name + '=')) {
                            encrytedPwd = decodeURIComponent(cookie.substring(pwd_name.length + 1));
                            $("#hdEncrytedPwd").val(encrytedPwd);
                        }
                    }
                   
                    $('#txtUsername').val(emailValue);
                    $('#txtPassword').val(encrytedPwd);
                    $("#remember").attr('checked', true);
                    $('#txtPassword').focus();
                }
                catch (e) {
                }
            }

        });

        function DoLogin() {
            var user = $('#txtUsername').val();
            var psswd = $('#txtPassword').val();
            var text = $("#btnLogin").val();
            var rem = $("#remember").is(':checked');
            var login_mode = "";
            $("#btnLogin").val('Kiểm tra...');
            $("#login_msg").html('');
            if ($("#hdEncrytedPwd").val() == psswd) login_mode = 'cookie';
            $.ajax({
                type: "POST",
                url: '/admin/users/authenticateuser',
                data: ({ userName: user, password: psswd, remember: rem, loginMode: login_mode }),
                dataType: "json",
                error: function (data) {
                    $("#btnLogin").val(text);
                },
                success: function (data) {
                    if (data.toString() == "True") {
                        var url = GetReturnUrlValue();
                        if (url == "") {
                            window.location = "/Admin/dashboard";
                        }
                        else {
                            window.location = unescape(url);
                        }
                       

                    } else {
                        $("#btnLogin").val(text);
                        $('#login_msg').show();
                        $("#login_msg").html('Tên đăng nhập hoặc mật khẩu sai...');
                    }
                },
                cache: false
            });      //end .ajax
        }
        function GetReturnUrlValue() {
            var result = "";
            var querystring = location.search.replace('?', '').split('&');
            var queryObj = {};
            for (var i = 0; i < querystring.length; i++) {
                var name = querystring[i].split('=')[0];
                if (name == "ReturnUrl") {
                    var value = querystring[i].split('=')[1];
                    result = value;
                }
            }
            return result;
        }
    </script>
</head>
<body style="background-color: #efefef;">
    <form id="loginForm" runat="server">   
    <div class="login-block">
        <div id="login_msg" style="position:relative;top:95px;background-color:Red;color:#fff;font-weight:bold;padding:10px 5px;text-align:center;display:none;"></div>
        <table class="login-table-container">
            <tbody>
                <tr class="row">
                    <td class="item-name">
                        Tên đăng nhập:
                    </td>
                </tr>
                <tr class="row">
                    <td class="item-value">
                    <input type="text" id="txtUsername" name="username" style="width:200px;" tabindex="1"  class="adminInput"/>
                    </td>
                </tr>
                <tr class="row">
                    <td class="item-name">
                       Mật khẩu :
                    </td>
                </tr>
                <tr class="row">
                    <td class="item-value">
                    <input type="password" id="txtPassword" name="password" style="width:200px;" tabindex="2" class="adminInput" />
                    </td>
                </tr>
                <tr class="row">
                    <td class="item-value">
                     <input type="checkbox" name="remember" id="remember" style="border:none; width:17px;height:17px;padding:0px;" tabindex="3" /> <label for="remember">Nhớ tên đăng nhập</label>
                    </td>
                </tr>
                <tr class="row">
                    <td class="message-error">
                    </td>
                </tr>
                <tr class="row">
                    <td>
                        <div class="buttons">
                        <input type="button" id="btnLogin" tabindex="4" class="adminButtonBlue" value="Đăng nhập" />
                        </div>
                    </td>
                </tr>
            </tbody>
        </table>
         <input type="hidden" id="hdEncrytedPwd" value="" />
    </div>
    </form>
</body>
</html>
